﻿// Copyright (c) 2010 Uittenbosch
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ServiceLocation;

namespace OAuth.Net.Mobile.Components
{

  /// <summary>
  /// A straight forward servicelocator.
  /// For the serviceLocator to work it needs already constructed
  /// objects from the types it needs to handle.
  /// It won't construct the objects as other ServiceLocators might do.
  /// e.g.
  /// 
  /// BasicServiceLocator injector = new BasicServiceLocator();
  ///
  /// injector.AddComponent<ISigningProvider, HmacSha1SigningProvider>("signing.provider:HMAC-SHA1", new HmacSha1SigningProvider());
  /// injector.AddComponent<INonceProvider, GuidNonceProvider>(typeof(INonceProvider).ToString(), new GuidNonceProvider());     
  /// ServiceLocator.SetLocatorProvider(() => injector);
  /// </summary>
  public class BasicServiceLocator : ServiceLocatorImplBase
  {
    /// <summary>
    /// A dictionary to hold all instanciated services
    /// </summary>
    private Dictionary<string, ServiceContainerItem> _storage;

    /// <summary>
    /// Constructor
    /// </summary>
    public BasicServiceLocator()
    {
      // Create the storage
      _storage = new Dictionary<string, ServiceContainerItem>();
    }

    /// <summary>
    /// Add a new component.
    /// </summary>
    /// <typeparam name="I">Interface type of the required object</typeparam>
    /// <typeparam name="T">Instance type of the required object</typeparam>
    /// <param name="key">A key to locate it</param>
    /// <param name="instance">An instance of the required object</param>
    public void AddComponent<I, T>(string key, object instance) where T : class
    {
      //Add it to the storage
      _storage.Add(key, new ServiceContainerItem()
      {
        Instance = instance,
        InstanceType = typeof(T),
        InterfaceType = typeof(I)
      });
    }

    /// <summary>
    /// Get a list of all object of the desired type
    /// </summary>
    /// <param name="serviceType">The type of object required</param>
    /// <returns></returns>
    protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
    {
      List<object> l = new List<object>();
      foreach (ServiceContainerItem i in _storage.Values)
      {
        // If the save, add it to the list
        if (serviceType.ToString() == i.InterfaceType.ToString())
          l.Add(i.Instance);
      }
      return l.ToArray();
    }

    /// <summary>
    /// Get a specific instance of the required type
    /// </summary>
    /// <param name="serviceType">Type of object</param>
    /// <param name="key">Key to find it with</param>
    /// <returns></returns>
    protected override object DoGetInstance(Type serviceType, string key)
    {
      if (string.IsNullOrEmpty(key))
      {
        key = serviceType.ToString();
      }
      return ((ServiceContainerItem)_storage[key]).Instance;
    }
  }
}